package com.acce.gxsc.page.registerCar.addGoodsInfo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.acce.gxsc.R;
import com.acce.gxsc.common.App;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.page.registerCar.selectGoods.SelectGoodsActivity;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.utils.ToastUtils;
import com.acce.sdk.widget.AcceBar;
import com.acce.sdk.widget.EditWithTitle;

import java.text.MessageFormat;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by huxley on 2017/5/26.
 */

public class AddGoodsInfoActivity extends BaseMvpActivity<AddGoodsInfoView, AddGoodsInfoPresenter> implements AddGoodsInfoView {

    @Bind(R.id.accebar)
    AcceBar       mAccebar;
    @Bind(R.id.ewt_goods_name)
    EditWithTitle mEwtGoodsName;
    @Bind(R.id.ewt_packaging)
    EditWithTitle mEwtPackaging;
    @Bind(R.id.ewt_num_1)
    EditWithTitle mEwtNum1;
    @Bind(R.id.ewt_num_2)
    EditWithTitle mEwtNum2;
    @Bind(R.id.ewt_weight)
    EditWithTitle mEwtWeight;
    @Bind(R.id.ewt_total_weight)

    EditWithTitle mEwtTotalWeight;
    @Bind(R.id.ewt_explan)
    EditWithTitle mEwtExplain;

    @Override
    protected AddGoodsInfoPresenter createPresenter() {
        return new AddGoodsInfoPresenter();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_goods_info);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        mEwtPackaging.setSpinnerData(Tools.getStringList(R.array.option_packaging));
        if(!App.isDongGuanShiChang){
            mEwtExplain.setVisibility(View.GONE);
        }
    }

    String goodsId;
    @OnClick(R.id.btn_save)
    public void onClick() {
        if (StringUtils.isEmpty(goodsId)) {
            ToastUtils.show("未选择商品！");
            return;
        }
        String[] goodsName = mEwtGoodsName.getContent().split("/");
        String goodsPackaging = mEwtPackaging.getContent();
        String goodNum1 = mEwtNum1.getContent();
        String goodNum2 = mEwtNum2.getContent();
        String weight = mEwtWeight.getContent();
        String explain=mEwtExplain.getContent();
        String totalWeight = mEwtTotalWeight.getContent();
//        if (goodsName.length < 2 || !StringUtils.areNotEmpty(goodsName[0], goodsName[1], goodsPackaging, goodNum1, goodNum2, weight, totalWeight)) {
//            ToastUtils.show("请完成信息输入！");
//            return;
//        }
        Intent intent = new Intent();
        intent.putExtra("goodsType", goodsName[0]);
        intent.putExtra("goodsName", goodsName[1]);
        intent.putExtra("goodsPackaging", goodsPackaging);
        intent.putExtra("goodNum1", goodNum1);
        intent.putExtra("goodNum2", goodNum2);
        intent.putExtra("weight", weight);
        intent.putExtra("totalWeight", totalWeight);
        intent.putExtra("goodsId", goodsId);
        intent.putExtra("explain",explain);
        setResult(RESULT_OK, intent);
        finish();
    }

    @OnClick({R.id.ll_goods_name, R.id.btn_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_goods_name:
                Intent companyInfoIntent = new Intent(this, SelectGoodsActivity.class);
                startActivityForResult(companyInfoIntent, 1001);
                break;
            case R.id.btn_save:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }

        switch (requestCode) {
            case 1001:
                String father = data.getStringExtra("father");
                String son = data.getStringExtra("son");
                goodsId = data.getStringExtra("goodsId");
                mEwtGoodsName.setContent(MessageFormat.format("{0}/{1}", father, son));
                break;
        }
    }
}
